package com.maizhi.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maizhi.controller.menuController;
import com.maizhi.dao.BaseDaoI;
import com.maizhi.httpmodel.EasyuiTreeNode;
import com.maizhi.httpmodel.MenuModel;
import com.maizhi.model.Menu;
import com.maizhi.model.User;
import com.maizhi.service.MenuServiceI;
import com.maizhi.util.MenuComparator;

@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements
		MenuServiceI {
	
	private Logger log = Logger.getLogger(MenuServiceImpl.class);
	
	BaseDaoI<Menu> baseDao;

	public BaseDaoI<Menu> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(BaseDaoI<Menu> baseDao) {
		this.baseDao = baseDao;
	}

	@Transactional(readOnly = true)
	public List<MenuModel> treeGrid(String id) {
		List<MenuModel> treegrid = new ArrayList<MenuModel>();
		String hql = "from Menu t where t.menu.id is null order by t.seq";
		if (id != null && !id.trim().equals("")) {
			hql = "from Menu t where t.menu.id = '" + id + "' order by t.seq";
		}
		List<Menu> menus = baseDao.find(hql);
		for (Menu menu : menus) {
			MenuModel m = new MenuModel();
			BeanUtils.copyProperties(menu, m);
			m.setSrc(menu.getUrl());
			m.setIconCls(menu.getImg());
			m.setState(menu.getStatus().toString());
			if (menu.getMenu()!= null) {
				m.setParentId(menu.getMenu().getId());
				m.setParentText(menu.getMenu().getText());
			}
			m.setIconCls(menu.getImg());
			if (menu.getMenus() != null && menu.getMenus().size() > 0) {
				m.setState("closed");
			}
			treegrid.add(m);
		}
		return treegrid;
	}
	
	@Cacheable(value = "MenuCache", key = "'tree'+#id")
	@Transactional(readOnly = true)
	public List<EasyuiTreeNode> tree(String id) {
		String hql = "from Menu t where t.menu.id is null order by t.seq";
		if (id != null && !id.trim().equals("")) {
			hql = "from Menu t where t.menu.id ='" + id + "' order by t.seq";
		}
		List<Menu> menus = baseDao.find(hql);
		List<EasyuiTreeNode> tree = new ArrayList<EasyuiTreeNode>();
		for (Menu menu : menus) {
			tree.add(tree(menu, false));
		}
		return tree;
	}

	private EasyuiTreeNode tree(Menu menu, boolean recursive) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(menu.getId());
		node.setText(menu.getText());
		node.setIconCls(menu.getImg());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("src", menu.getUrl());
		node.setAttributes(attributes);
		if (menu.getMenus() != null && menu.getMenus().size() > 0) {
			node.setState("closed");
			if (recursive) {// 递归查询子节点
				List<Menu> symenuList = new ArrayList<Menu>(menu.getMenus());
				Collections.sort(symenuList, new MenuComparator());// 排序
				List<EasyuiTreeNode> children = new ArrayList<EasyuiTreeNode>();
				for (Menu m : symenuList) {
					EasyuiTreeNode t = tree(m, true);
					children.add(t);
				}
				node.setChildren(children);
			}
		}
		return node;
	}
	
	@CacheEvict(value = "MenuCache", allEntries = true)
	@Transactional 
	public MenuModel add(MenuModel m) {
		Menu menu = new Menu();
		BeanUtils.copyProperties(m, menu);
		menu.setImg(m.getIconCls());
		menu.setUrl(m.getSrc());
		menu.setStatus(1);
		menu.setTitle(m.getText());
		if (m.getParentId() != null && !m.getParentId().trim().equals("")) {
			Menu pm = baseDao.load(Menu.class, m.getParentId());
			menu.setMenu(pm);
		}
		baseDao.save(menu);
		return m;
	}

	@CacheEvict(value = "MenuCache", allEntries = true)
	@Transactional 
	public void del(MenuModel m) {
		Menu menu = baseDao.get(Menu.class, m.getId());
		if (menu != null) {
			recursiveDelete(menu);
		}
	}

	@Transactional 
	private void recursiveDelete(Menu menu) {
		if (menu.getMenus() != null && menu.getMenus().size() > 0) {
			Set<Menu> symenus = menu.getMenus();
			for (Menu t : symenus) {
				recursiveDelete(t);
			}
		}
		baseDao.delete(menu);
	}

	@CacheEvict(value = "MenuCache", allEntries = true)
	@Transactional 
	public MenuModel edit(MenuModel m) {
		Menu menu = baseDao.get(Menu.class, m.getId());
		if (m != null) {
			BeanUtils.copyProperties(m, menu);
			menu.setImg(m.getIconCls());
			menu.setUrl(m.getSrc());
			if (!menu.getId().equals("0")) {// 根节点不可以修改上级节点
				menu.setMenu(baseDao.get(Menu.class, m.getParentId()));
			}
		}
		return m;
	}

}
